const db = require('../db/index')

const moment = require('moment')

//获取个人的信息
exports.get_minadminInfo = function (req, res) {
    // console.log(req.user);
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('获取失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: results
        })
    })
}


//修改个人信息
exports.update_minAdminInfo = function (req, res) {
    console.log(req.body);
    if (req.body.phone && req.body.email) {
        db.query('update minadmin_info set ? where num=?', [{ phone: req.body.phone, email: req.body.email }, req.user.username], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 1) return res.cc('修改失败')
            res.send({
                status: 0,
                message: '修改成功'
            })
        })
    }
    if (!req.body.phone && req.body.email) {
        db.query('update minadmin_info set ? where num=?', [{ email: req.body.email }, req.user.username], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 1) return res.cc('修改失败')
            res.send({
                status: 0,
                message: '修改成功'
            })
        })
    }
    if (req.body.phone && !req.body.email) {
        db.query('update teacher set ? where teacher_num=?', [{ phone: req.body.phone }, req.user.username], function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 1) return res.cc('修改失败')
            res.send({
                status: 0,
                message: '修改成功'
            })
        })
    }
    if (!req.body.phone && !req.body.email) {
        res.cc('你没有提交的修改信息啊')
    }
}

//修改密码
exports.update_minAdminPwd = function (req, res) {
    // console.log(req.body);
    db.query('update minadmin set ? where username=?', [{ password: req.body.checkPass }, req.user.username], function (err, results) {
        if (err) return res.cc(err)
        if (results.affectedRow < 1) return res.cc('修改失败')
        res.send({
            status: 0,
            message: '修改成功'

        })
    })
}

//添加学生
exports.addstudent = function (req, res) {
    // console.log(req.user);
    // console.log(req.body);
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('不存在这人')
        // console.log(results);
        const userinfo = {
            student_name: req.body.name,
            student_num: req.body.num,
            student_class: req.body.region,
            student_college: results[0].collage
        }
        const user = {
            username: req.body.num,
            password: req.body.num
        }
        db.query('select * from student where student_num=?', req.body.num, function (err, resultss) {
            if (err) return res.cc(err)
            if (resultss.length < 1) {
                db.query('insert into student set ?', userinfo, function (err, result) {
                    if (err) return res.cc(err)
                    if (result.affectedRow < 1) return res.cc('个人信息添加失败')
                    db.query('insert into user set ?', user, function (err, resul) {
                        if (err) return res.cc(err)
                        if (resul.affectedRow < 1) return res.cc('登录信息添加失败')
                        res.send({
                            status: 0,
                            message: '添加成功'
                        })
                    })
                })
            } else {
                res.send({
                    status: 1,
                    message: '该学号已经存在'
                })
            }
        })
    })
}

//添加教师
exports.addteacher = function (req, res) {
    // console.log(req.user);
    // console.log(req.body);
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('不存在这人')
        // console.log(results);
        const userinfo = {
            teacher_name: req.body.name,
            teacher_num: req.body.num,
            teacher_Workplace: req.body.Workplace,
            teacher_degree: req.body.degree,
            teacher_college: results[0].collage
        }
        const user = {
            username: req.body.num,
            password: req.body.num
        }
        db.query('insert into teacher set ?', userinfo, function (err, result) {
            if (err) return res.cc(err)
            if (result.affectedRow < 1) return res.cc('个人信息添加失败')
            db.query('insert into teacher_login set ?', user, function (err, resul) {
                if (err) return res.cc(err)
                if (resul.affectedRow < 1) return res.cc('登录信息添加失败')
                res.send({
                    status: 0,
                    message: '添加成功'
                })
            })
        })
    })
}


//发布公告
exports.send_notice = function (req, res) {
    // console.log(req.user[0].username);
    db.query('select * from minAdmin_info where num=?', req.user.username, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('错误')
        // console.log(result);
        const content = {
            message: req.body.message,
            date: new Date(),
            collage: result[0].collage,
            madmin_num: req.user.username,
            title: req.body.title
        }
        db.query('insert into notice set ?', content, function (err, results) {
            if (err) return res.cc(err)
            if (results.affectedRow < 1) return res.cc('发布失败')
            res.send({
                status: 0,
                message: '发布成功'
            })
        })
    })
}

//删除公告
exports.dele_notice = function (req, res) {
    db.query('update notice set dele_status=1 where id=?', req.body.id, function (err, results) {
        if (err) return res.cc(err)
        if (results.affectedRow < 1) return res.cc('删除失败')
        res.send({
            status: 0,
            message: '删除成功',
        })
    })
}

//获取自己发布的公告
exports.get_notice = function (req, res) {
    // console.log(req.user);
    db.query('select * from notice where dele_status=0 and madmin_num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有发布公告')
        res.send({
            status: 0,
            message: '公告来了',
            data: results
        })
    })
}

//获取教师提交的毕设题
exports.teacher_topic = function (req, res) {
    const arr = []
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('查询失败')
        db.query('select * from teacher where teacher_college=?', result[0].collage, function (err, resul) {
            if (err) return res.cc(err)
            if (resul.length < 1) return res.cc('获取失败')
            // console.log(resul);
            resul.forEach(val => {
                arr.push(val.teacher_num)
            })
            db.query('select * from topic where teacher_id in (?) and pass=0', [arr], function (err, results) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('没有新提交的信息')
                res.send({
                    status: 0,
                    message: '教师的课题',
                    data: results
                })
            })
        })

    })
}

//通过教师提交的毕设题
exports.pass_topic = function (req, res) {
    // console.log(req.body);
    db.query('update topic set pass=1 where id=?', req.body.id, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('修改失败')
        res.send({
            status: 0,
            message: '修改成功',
        })
    })
}

//获取本学院学生信息
exports.get_allstudent = function (req, res) {
    // 一页显示的数据条数
    const pageSize = req.query.limit
    //当前的页数
    const pageCurrent = req.query.current
    if (!req.query.region && !req.query.user) {
        db.query('select * from minadmin_info where num=?', req.user.username, function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('查询失败1')
            // console.log(result[0].collage);
            db.query('select * from student where student_college=? order by student_num', result[0].collage, function (err, results) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('获取失败')
                // console.log(results);
                // 数据的总数
                const total = results.length
                const datalist = results.slice((pageCurrent - 1) * pageSize, pageCurrent * pageSize)
                res.send({
                    status: 0,
                    message: '获取成功',
                    data: { datalist, total }
                })
            })
        })
    }

    if (req.query.region && !req.query.user) {
        db.query('select * from minadmin_info where num=?', req.user.username, function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('查询失败1')
            // console.log(result[0].collage);
            db.query('select * from student where student_class=? and student_college=? order by student_num', [req.query.region, result[0].collage], function (err, results) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('获取失败')
                // console.log(results);
                // 数据的总数
                const total = results.length
                const datalist = results.slice((pageCurrent - 1) * pageSize, pageCurrent * pageSize)
                res.send({
                    status: 0,
                    message: '获取成功',
                    data: { datalist, total }
                })
            })
        })
    }
    if (!req.query.region && req.query.user) {
        db.query('select * from minadmin_info where num=?', req.user.username, function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('查询失败1')
            // console.log(result[0].collage);
            db.query('select * from student where student_num=? and student_college=? order by student_num', [req.query.user, result[0].collage], function (err, results) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('获取失败')
                // console.log(results);
                // 数据的总数
                const total = results.length
                const datalist = results.slice((pageCurrent - 1) * pageSize, pageCurrent * pageSize)
                res.send({
                    status: 0,
                    message: '获取成功',
                    data: { datalist, total }
                })
            })
        })
    }
    if (req.query.region && req.query.user) {
        db.query('select * from minadmin_info where num=?', req.user.username, function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('查询失败1')
            // console.log(result[0].collage);
            db.query('select * from student where student_num=? and student_college=? order by student_num', [req.query.user, result[0].collage], function (err, results) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('获取失败')
                // console.log(results);
                // 数据的总数
                const total = results.length
                const datalist = results.slice((pageCurrent - 1) * pageSize, pageCurrent * pageSize)
                res.send({
                    status: 0,
                    message: '获取成功',
                    data: { datalist, total }
                })
            })
        })
    }
}

//按条件挑选教师
exports.choose_teacher = function (req, res) {
    // console.log(req.body.num);
    if (req.body.user) {
        db.query('select * from teacher where teacher_num=?', req.body.user, function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('查询失败')
            res.send({
                status: 0,
                message: "获取成功",
                data: result
            })
        })
    }
    if (!req.body.user) {
        db.query('select * from teacher', req.body.user, function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('查询失败')
            res.send({
                status: 0,
                message: "获取成功",
                data: result
            })
        })
    }
}

//获取单个学生信息
exports.get_student = function (req, res) {
    db.query('select * from student where id=?', req.query.id, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('获取失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: results
        })
    })
}

//获取本学院教师
exports.get_allteacher = function (req, res) {
    // console.log(req.query);
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('查询失败')
        // console.log(result[0].collage);
        db.query('select * from teacher where teacher_college=?', result[0].collage, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('获取失败')
            // console.log(results);

            // 数据总条数
            const total = results.length
            //当前页码
            const pageCurrent = req.query.current
            // 每页显示个数
            const pageSize = req.query.limit

            const datalist = results.slice((pageCurrent - 1) * pageSize, pageCurrent * pageSize)

            res.send({
                status: 0,
                message: '获取成功',
                data: { datalist, total }
            })
        })
    })
}

//获取单个教师信息
exports.get_teacher = function (req, res) {
    db.query('select * from teacher where id=?', req.query.id, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('获取失败')
        // console.log(results);
        res.send({
            status: 0,
            message: '获取成功',
            data: results
        })
    })
}

//获取教师旧密码
exports.get_teacherpwd = function (req, res) {
    // console.log(req.body);
    db.query('select * from teacher_login where username=?', req.body.teacher_num, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('查询失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: results
        })
    })
}

//修该教师密码
exports.update_teacherpwd = function (req, res) {
    // console.log(req.body);
    db.query('update teacher_login set password=? where password=?', [req.body.new, req.body.old], function (err, results) {
        if (err) return res.cc(err)
        if (results.affectedRow < 1) return res.cc('修改失败')
        res.send({
            status: 0,
            message: '修改成功'
        })
    })
}

//获取学生旧密码
exports.get_pwd = function (req, res) {
    // console.log(req.body);
    db.query('select * from user where username=?', req.body.student_num, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('查询失败')
        res.send({
            status: 0,
            message: '获取成功',
            data: results
        })
    })
}

//修该学生密码
exports.update_pwd = function (req, res) {
    // console.log(req.body);
    db.query('update user set password=? where password=?', [req.body.new, req.body.old], function (err, results) {
        if (err) return res.cc(err)
        if (results.affectedRow < 1) return res.cc('修改失败')
        res.send({
            status: 0,
            message: '修改成功'
        })
    })
}


//获取能够分配评阅教师的学生
exports.choose_topic = function (req, res) {
    const arr = []
    const arr1 = []
    const arr2 = []
    db.query('select collage from minadmin_info where num=?', req.user.username, function (err, resul) {
        if (err) return res.cc(err)
        if (resul.length < 1) return res.cc('获取失败')
        // console.log(resul);
        db.query('select * from report where reply_status=1', function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('还没有学生选题')
            results.forEach(val => {
                arr.push(val.student_num)
            })
            // console.log(arr);
            db.query('select * from student where student_num in (?) and student_college=?', [arr, resul[0].collage], function (err, result) {
                if (err) return res.cc(err)
                if (result.length < 1) return res.cc('还没有学生')
                // console.log(result);
                result.forEach(val => {
                    arr1.push(val.student_num)
                })
                // console.log(arr1);
                // --------------------------------------------------
                db.query('select * from other_teacher_student where student_num in (?)', [arr1], function (err, resulta) {
                    if (err) return res.cc(err)
                    if (resulta.length < 1) {
                        db.query('select * from teacher_student where studentId in (?)', [arr1], function (err, resultss) {
                            if (err) return res.cc(err)
                            if (resultss.length < 1) return res.cc('查询失败')
                            // console.log(resultss);
                            resultss.forEach(val => {
                                arr2.push(val.teacherId)
                            })
                            // console.log(arr2);
                            db.query('select * from teacher where teacher_college=? and teacher_num in (?)', [resul[0].collage, arr2], function (err, resu) {
                                if (err) return res.cc(err)
                                // console.log(resu);
                                if (resu.length < 1) return res.cc('还没有教师')
                                result.forEach(value => {
                                    resultss.forEach(val => {
                                        if (value.student_num == val.studentId) {
                                            Object.assign(value, { teacher_num: val.teacherId })
                                        }
                                    })
                                })
                                // console.log(result);
                                result.forEach(value => {
                                    resu.forEach(val => {
                                        if (value.teacher_num == val.teacher_num) {
                                            Object.assign(value, { teacher_name: val.teacher_name })
                                        }
                                    })
                                })
                                res.send({
                                    status: 0,
                                    message: '查到了1',
                                    data: result
                                })
                            })
                        })
                    }
                    else {
                        var arr3 = []
                        resulta.forEach(val => [
                            arr3.push(val.teacher_num)
                        ])
                        db.query('select * from teacher where teacher_num in (?)', [arr3], function (err, resultb) {
                            if (err) return res.cc(err)
                            if (resultb.length < 1) return res.cc('没有教师信息')
                            db.query('select * from teacher_student where studentId in (?)', [arr1], function (err, resultss) {
                                if (err) return res.cc(err)
                                if (resultss.length < 1) return res.cc('查询失败')
                                // console.log(resultss);
                                resultss.forEach(val => {
                                    arr2.push(val.teacherId)
                                })
                                // console.log(arr2);
                                db.query('select * from teacher where teacher_college=? and teacher_num in (?)', [resul[0].collage, arr2], function (err, resu) {
                                    if (err) return res.cc(err)
                                    if (resu.length < 1) return res.cc('还没有教师')
                                    // console.log(resu);
                                    result.forEach(value => {
                                        resultss.forEach(val => {
                                            if (value.student_num == val.studentId) {
                                                Object.assign(value, { teacher_num: val.teacherId })
                                            }
                                        })
                                    })
                                    // console.log(result);
                                    result.forEach(value => {
                                        resu.forEach(val => {
                                            if (value.teacher_num == val.teacher_num) {
                                                Object.assign(value, { teacher_name: val.teacher_name })
                                            }
                                        })
                                    })
                                    // console.log(result);

                                    result.forEach(value => {
                                        resulta.forEach(val => {
                                            if (value.student_num == val.student_num) {
                                                Object.assign(value, { lookteacher_num: val.teacher_num })
                                            }
                                        })
                                    })
                                    // console.log(result);
                                    result.forEach(value => {
                                        resultb.forEach(val => {
                                            if (value.lookteacher_num == val.teacher_num) {
                                                Object.assign(value, { lookteacher_name: val.teacher_name })
                                            }
                                        })
                                    })
                                    res.send({
                                        status: 0,
                                        message: '查到了',
                                        data: result
                                    })
                                })
                            })
                        })
                    }

                })
            })

        })
    })
}

//获取本学院教师信息
exports.get_collageteacher = function (req, res) {
    // console.log(req.user);
    db.query('select collage from minadmin_info where num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('获取失败')
        // console.log(results);
        db.query('select * from teacher where teacher_college=?', results[0].collage, function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('获取失败')
            res.send({
                status: 0,
                message: '获取成功',
                data: result
            })
        })
    })
}

// 添加评阅教师
exports.insert_teacher = function (req, res) {
    // console.log(req.body);
    db.query('select * from other_teacher_student where student_num=?', req.body.student_num, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1)
            return db.query('insert into other_teacher_student set ? ', { student_num: req.body.student_num, teacher_num: req.body.teacher_num }, function (err, results) {
                if (err) return res.cc(err)
                if (results.affectedRow < 1) return res.cc('选择失败')
                res.send({
                    status: 0,
                    message: '选择成功'
                })
            })
        return res.send({
            status: 0,
            message: '你已经选择了'
        })
    })
}

//添加评阅小组
exports.insert_group = function (req, res) {
    // console.log(req.body);
    db.query('select collage from minadmin_info where num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('获取失败')
        const obj = {
            group_leader: req.body.region,
            group_num: req.body.user,
            group_collage: results[0].collage,
            group_style: req.body.style,
            group_address: req.body.address,
            teacher_num: req.body.teacher_num,
            time: req.body.value1
        }
        // console.log(results);
        db.query('select * from teacher where teacher_college=?', results[0].collage, function (err, resul) {
            if (err) return res.cc(err)
            if (resul.length < 1) return res.cc('获取失败')
            const arr = []
            resul.forEach(val => {
                arr.push(val.teacher_num)
            })
            db.query('select * from teacher_group where teacher_num in (?)', [arr], function (err, resu) {
                if (err) return res.cc(err)
                // console.log(resu);
                if (resu.length < 1) {
                    return db.query('insert into teacher_group set ?', obj, function (err, result) {
                        if (err) return res.cc(err)
                        if (result.affectedRow < 1) return res.cc('添加失败')
                        res.send({
                            status: 0,
                            message: '添加成功',
                        })
                    })
                }
                if (resu.length > 0) {
                    // console.log(req.body.style);
                    const fil = resu.filter(val => {
                        return val.teacher_num == req.body.teacher_num
                    })
                    // console.log(fil);
                    const booler = fil.some(val => {
                        // console.log(val.group_style);
                        return val.group_style == req.body.style
                    })
                    // console.log(booler);
                    if (booler) {
                        res.send({
                            status: 0,
                            message: '该类型已经被添加',
                        })
                    }
                    if (!booler) {
                        // console.log(req.body.style);
                        const filt = resu.filter(value => {
                            return value.group_style == req.body.style
                        })
                        // console.log(filt);

                        const boolea = filt.filter(value => {
                            return value.group_num == req.body.user
                        })
                        // console.log(boolea);
                        if (boolea) {
                            const bool = boolea.some(val => {
                                return val.group_leader == '是'
                            })
                            if (!bool && req.body.region == '是' || req.body.region == '否') {
                                db.query('insert into teacher_group set ?', obj, function (err, re) {
                                    if (err) return res.cc(err)
                                    if (re.affectedRow < 1) return res.cc('添加失败')
                                    res.send({
                                        status: 0,
                                        message: '添加成功',
                                    })
                                })
                            } else {
                                res.send({
                                    status: 1,
                                    message: '该小组有组长了',
                                })
                            }
                        }
                    }

                }

            })
        })
    })
}

//获取答辩小组组长
exports.get_groupTeacher = function (req, res) {
    // console.log(req.user);
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('没有管理员啊')
        // console.log(result);
        db.query('select * from teacher where teacher_college=?', result[0].collage, function (err, resul) {
            if (err) return res.cc(err)
            if (resul.length < 1) return res.cc('该学院没有查到教师')
            // console.log(resul);
            const arr = []
            db.query('select * from teacher_group where group_collage=? and  group_leader="是" and group_style="开题答辩小组"', result[0].collage, function (err, results) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('没有开题答辩的教师')
                // console.log(results);
                resul.forEach(val => {
                    results.forEach(value => {
                        if (val.teacher_num == value.teacher_num) {
                            Object.assign(value, { teacher_name: val.teacher_name })
                        }
                    })
                })
                res.send({
                    status: 0,
                    message: '小组组长来了',
                    data: results
                })
            })
        })
    })
}

//确认给开题答辩组长分配学生的按钮
exports.distribution_student = function (req, res) {
    // console.log(req.body[1]);
    const time = new Date(req.body[1].time)
    const rest = moment(time).format('yyyy-MM-DD hh:mm:ss')
    // console.log(rest);
    req.body[0].forEach(val => {
        // console.log(val);
        db.query('insert into student_group set ? ',
            {
                student_num: val.student_num,
                group_address: req.body[1].group_address,
                group_num: req.body[1].group_num,
                group_style: req.body[1].group_style,
                collage: req.body[1].group_collage,
                time: rest,
                // time: req.
            }, function (err, results) {
                if (err) return res.cc(err)
                if (results.affectedRow < 1) return res.cc('没有')
            })
    })
    res.send({
        status: 0,
        message: '成功',

    })
}


//确认给毕业答辩组长分配学生的按钮
exports.distribution_paper = function (req, res) {
    // console.log(req.body);
    const time = new Date(req.body[1].time)
    const rest = moment(time).format('yyyy-MM-DD hh:mm:ss')
    req.body[0].forEach(val => {
        db.query('insert into student_group set ? ',
            {
                student_num: val.student_num,
                group_num: req.body[1].group_num,
                group_address: req.body[1].group_address,
                group_style: req.body[1].group_style,
                collage: req.body[1].group_collage,
                time: rest,
            }, function (err, results) {
                if (err) return res.cc(err)
                if (results.affectedRow < 1) return res.cc('没有')
            })
    })
    res.send({
        status: 0,
        message: '成功',

    })
}

//获取毕业答辩小组组长
exports.groupteacher = function (req, res) {
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('没有管理员啊')
        // console.log(result);
        db.query('select * from teacher where teacher_college=?', result[0].collage, function (err, resul) {
            if (err) return res.cc(err)
            if (resul.length < 1) return res.cc('该学院没有查到教师')
            const arr = []
            db.query('select * from teacher_group where group_collage=? and group_leader="是" and group_style="毕业论文答辩小组"', result[0].collage, function (err, results) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('没有毕业答辩的教师')
                resul.forEach(val => {
                    results.forEach(value => {
                        if (val.teacher_num == value.teacher_num) {
                            Object.assign(value, { teacher_name: val.teacher_name })
                        }
                    })
                })
                // console.log(results);
                res.send({
                    status: 0,
                    message: '小组组长来了',
                    data: results
                })
            })
        })
    })
}


//获取已经分配的学生信息
exports.distribution_studentinfo = function (req, res) {
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('查询失败')
        db.query('select * from student_group where collage=? and group_style="开题答辩小组"', results[0].collage, function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('查询失败')
            res.send({
                status: 0,
                message: '获取成功',
                data: result
            })
        })
    })

}

//获取已经毕业答辩分配的学生信息
exports.distribution_paperinfo = function (req, res) {
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('查询失败')
        db.query('select * from student_group where collage=? and group_style="毕业论文答辩小组"', results[0].collage, function (err, result) {
            if (err) return res.cc(err)
            if (result.length < 1) return res.cc('查询失败')
            res.send({
                status: 0,
                message: '获取成功',
                data: result
            })
        })
    })

}

//查看学生毕设成绩
exports.student_score = function (req, res) {
    // console.log(req.query);
    const arr = []
    const pageSize = req.query.pageSize
    const current = req.query.current
    var college

    if (!req.query.region && !req.query.user) {
        db.query('select * from minadmin_info where num=?', req.user.username, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有信息')
            college = results[0].collage
            // console.log(results);
            db.query('select * from student where student_college=?', results[0].collage, function (err, result) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('没有信息')
                // console.log(result);
                result.forEach(val => {
                    arr.push(val.student_num)
                })
                db.query('select * from article where article_type="终稿" and article_dele=0 and article_studentId in (?)', [arr], function (err, resul) {
                    if (err) return res.cc(err)
                    // console.log(resul);
                    resul.forEach(val => {
                        Object.assign(val, { allScore: val.article_teacherscore * 0.3 + val.look_teacherScore * 0.3 + val.reply_score * 0.4 })
                    })
                    resul.forEach(value => {
                        result.forEach(val => {
                            if (value.article_studentId == val.student_num)
                                Object.assign(value, { student_name: val.student_name, student_class: val.student_class, student_college: val.student_college })
                        })
                    })
                    const total = resul.length
                    const datalist = resul.slice((current - 1) * pageSize, current * pageSize)
                    // console.log(college);
                    res.send({
                        status: 0,
                        message: '学生的毕设成绩来了',
                        data: { datalist, total, college }
                    })
                })
            })
        })
    }

    if (req.query.region && !req.query.user) {
        db.query('select * from minadmin_info where num=?', req.user.username, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有信息')
            // console.log(results);
            db.query('select * from student where student_class=? and student_college=?', [req.query.region, results[0].collage], function (err, result) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('没有信息')
                // console.log(result);
                result.forEach(val => {
                    arr.push(val.student_num)
                })
                db.query('select * from article where article_type="终稿" and article_dele=0 and article_studentId in (?)', [arr], function (err, resul) {
                    if (err) return res.cc(err)
                    if (resul.length < 1) return res.cc('该专业还没有终稿')
                    // console.log(resul);
                    resul.forEach(val => {
                        Object.assign(val, { allScore: val.article_teacherscore * 0.3 + val.look_teacherScore * 0.3 + val.reply_score * 0.4 })
                    })
                    resul.forEach(value => {
                        result.forEach(val => {
                            if (value.article_studentId == val.student_num)
                                Object.assign(value, { student_name: val.student_name, student_class: val.student_class })
                        })
                    })
                    const total = resul.length
                    const datalist = resul.slice((current - 1) * pageSize, current * pageSize)
                    res.send({
                        status: 0,
                        message: '学生的毕设成绩来了',
                        data: { datalist, total, college }
                    })
                })
            })
        })
    }

    if (!req.query.region && req.query.user) {
        db.query('select * from minadmin_info where num=?', req.user.username, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有信息')
            // console.log(results);
            db.query('select * from student where student_num=? and student_college=?', [req.query.user, results[0].collage], function (err, result) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('没有信息')
                // console.log(result);
                result.forEach(val => {
                    arr.push(val.student_num)
                })
                db.query('select * from article where article_type="终稿"  and article_dele=0 and article_studentId in (?)', [arr], function (err, resul) {
                    if (err) return res.cc(err)
                    if (resul.length < 1) return res.cc('该学生没有终稿')
                    // console.log(resul);
                    resul.forEach(val => {
                        Object.assign(val, { allScore: val.article_teacherscore * 0.3 + val.look_teacherScore * 0.3 + val.reply_score * 0.4 })
                    })
                    resul.forEach(value => {
                        result.forEach(val => {
                            if (value.article_studentId == val.student_num)
                                Object.assign(value, { student_name: val.student_name, student_class: val.student_class })
                        })
                    })
                    const total = resul.length
                    const datalist = resul.slice((current - 1) * pageSize, current * pageSize)
                    res.send({
                        status: 0,
                        message: '学生的毕设成绩来了',
                        data: { datalist, total, college }
                    })
                })
            })
        })
    }
    if (req.query.region && req.query.user) {
        db.query('select * from minadmin_info where num=?', req.user.username, function (err, results) {
            if (err) return res.cc(err)
            if (results.length < 1) return res.cc('没有信息')
            // console.log(results);
            db.query('select * from student where student_num=? and student_class=? and student_college=?', [req.query.user, req.query.region, results[0].collage], function (err, result) {
                if (err) return res.cc(err)
                if (result.length < 1) return res.cc('没有该学生信息')
                // console.log(result);
                result.forEach(val => {
                    arr.push(val.student_num)
                })
                db.query('select * from article where article_type="终稿" and article_dele=0 and article_studentId in (?)', [arr], function (err, resul) {
                    if (err) return res.cc(err)
                    if (resul.length < 1) return res.cc('该学生没有终稿')
                    // console.log(resul);
                    resul.forEach(val => {
                        Object.assign(val, { allScore: val.article_teacherscore * 0.3 + val.look_teacherScore * 0.3 + val.reply_score * 0.4 })
                    })
                    resul.forEach(value => {
                        result.forEach(val => {
                            if (value.article_studentId == val.student_num)
                                Object.assign(value, { student_name: val.student_name, student_class: val.student_class })
                        })
                    })
                    const total = resul.length
                    const datalist = resul.slice((current - 1) * pageSize, current * pageSize)
                    res.send({
                        status: 0,
                        message: '学生的毕设成绩来了',
                        data: { datalist, total, college }
                    })
                })
            })
        })
    }
}

//获取能够参与论文答辩学生信息
exports.get_studentPaperinfo = function (req, res) {
    // console.log(req.user[0]);
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('查询失败')
        // console.log(result[0].collage);
        db.query('select * from report where reply_status=1', function (err, resul) {
            if (err) return res.cc(err)
            if (resul.length < 1) return res.cc('还没有学生通过答辩小组')
            const arr = []
            resul.forEach(val => {
                arr.push(val.student_num)
            })
            // console.log(arr);
            db.query('select * from student where student_num in (?) and student_college=?', [arr, result[0].collage], function (err, results) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('获取失败')
                // console.log(results);
                res.send({
                    status: 0,
                    message: '获取成功',
                    data: results
                })
            })

        })
    })
}

//获取能够参与开题报告答辩学生信息
exports.get_studentreportinfo = function (req, res) {
    // console.log(req.user[0]);
    db.query('select * from minadmin_info where num=?', req.user.username, function (err, result) {
        if (err) return res.cc(err)
        if (result.length < 1) return res.cc('查询失败')
        // console.log(result);
        // console.log(result[0].collage);
        db.query('select * from report where report_status=1', function (err, resul) {
            if (err) return res.cc(err)
            if (resul.length < 1) return res.cc('还没有学生通过教师')
            // console.log(resul);
            const arr = []
            resul.forEach(val => {
                arr.push(val.student_num)
            })
            // console.log(arr);
            db.query('select * from student where student_num in (?) and student_college=?', [arr, result[0].collage], function (err, results) {
                if (err) return res.cc(err)
                if (results.length < 1) return res.cc('获取失败')
                // console.log(results);
                res.send({
                    status: 0,
                    message: '获取成功',
                    data: results
                })
            })
        })
    })
}